home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / AOCE / Development Tools / Interfaces / PInterfaces / DigitalSignature.p < prev    next >
Encoding:
Text File  |  1993-11-10  |  15.2 KB  |  400 lines  |  [TEXT/MPS ]

  1. {***********************************************************
  2.  
  3. Created: Tuesday, August 17, 1993 4:37:24 PM
  4.  DigitalSignature.p
  5.  Pascal Interface to the Macintosh Libraries
  6.  
  7.   Copyright Apple Computer, Inc. 1990-1993
  8.   All rights reserved
  9.  
  10. ***********************************************************}
  11.  
  12.  
  13. {$IFC UNDEFINED UsingIncludes}
  14. {$SETC UsingIncludes:= 0}
  15. {$ENDC}
  16.  
  17. {$IFC NOT UsingIncludes}
  18.     UNIT DigitalSignature;
  19.     INTERFACE
  20. {$ENDC}
  21.  
  22. {$IFC UNDEFINED UsingDigitalSignature}
  23. {$SETC UsingDigitalSignature:= 1}
  24.  
  25. {$I+}
  26. {$SETC DigitalSignatureIncludes:= UsingIncludes}
  27. {$SETC UsingIncludes:= 1}
  28.  
  29.  
  30. {$IFC UNDEFINED UsingFiles}
  31. {$I $$Shell(PInterfaces)Files.p}
  32. {$ENDC}
  33.  
  34. {$IFC UNDEFINED UsingMemory}
  35. {$I $$Shell(PInterfaces)Memory.p}
  36. {$ENDC}
  37.  
  38. {$IFC UNDEFINED UsingTypes}
  39. {$I $$Shell(PInterfaces)Types.p}
  40. {$ENDC}
  41.  
  42. {$SETC UsingIncludes:= DigitalSignatureIncludes}
  43.  
  44.  
  45. { —————————————————————————— TRAP SELECTORS —————————————————————————— }
  46.  
  47. CONST
  48. kSIGNewContext                    = 1900;
  49. kSIGDisposeContext                = 1901;
  50.  
  51. kSIGSignPrepare                    = 1902;
  52. kSIGSign                        = 1903;
  53.  
  54. kSIGVerifyPrepare                = 1904;
  55. kSIGVerify                        = 1905;
  56.  
  57. kSIGDigestPrepare                = 1906;
  58. kSIGDigest                        = 1907;
  59.  
  60. kSIGProcessData                    = 1908;
  61.  
  62. kSIGShowSigner                    = 1909;
  63. kSIGGetSignerInfo                = 1910;
  64. kSIGGetCertInfo                    = 1911;
  65. kSIGGetCertNameAttributes        = 1912;
  66. kSIGGetCertIssuerNameAttributes    = 1913;
  67.  
  68. kSIGFileIsSigned                = 2500;
  69. kSIGSignFile                    = 2501;
  70. kSIGVerifyFile                    = 2502;
  71.  
  72.  
  73. { values of SIGNameAttributeType }
  74. kSIGCountryCode = 0;
  75. kSIGOrganization = 1;
  76. kSIGStreetAddress = 2;
  77. kSIGState = 3;
  78. kSIGLocality = 4;
  79. kSIGCommonName = 5;
  80. kSIGTitle = 6;
  81. kSIGOrganizationUnit = 7;
  82. kSIGPostalCode = 8;
  83.  
  84. TYPE
  85. SIGNameAttributeType = INTEGER;
  86.  
  87. {
  88. Certificate status codes returned in SIGCertInfo or SIGSignerInfo from
  89. either SIGGetCertInfo or SIGGetSignerInfo respectively. kSIGValid means that
  90. the certificate is currently valid. kSIGPending means the certificate is
  91. currently not valid - but will be.  kSIGExpired means the certificate has
  92. expired. A time is always associated with a SIGCertStatus.  In each case the
  93. time has a specific interpretation.  When the status is kSIGValid the time is
  94. when the certificate will expire. When the status is kSIGPending the time is
  95. when the certificate will become valid. When the status is kSIGExpired the time
  96. is when the certificate expired. In the SIGCertInfo structure, the startDate
  97. and endDate fields hold the appropriate date information.  In the SIGSignerInfo
  98. structure, this information is provided in the certSetStatusTime field. In the
  99. SIGSignerInfo struct, the status time is actually represented by the SIGSignatureStatus
  100. field which can contain any of the types below. NOTE: The only time you will get
  101. a kSIGInvalid status is when it pertains to a SIGSignatureStatus field and only when
  102. you get a signature that was created after the certificates expiration date, something
  103. we are not allowing on the Mac but that may not be restricted on other platforms. Also,
  104. it will not be possible to get a kSIGPending value for SIGSignatureStatus on the Mac but
  105. possibly allowed by other platforms.
  106. }
  107.  
  108. { Values for SIGCertStatus or SIGSignatureStatus }
  109. CONST
  110. kSIGValid = 0;                { possible for either a SIGCertStatus or SIGSignatureStatus }
  111. kSIGPending = 1;            { possible for either a SIGCertStatus or SIGSignatureStatus }
  112. kSIGExpired = 2;            { possible for either a SIGCertStatus or SIGSignatureStatus }
  113. kSIGInvalid = 3;            { possible only for a SIGSignatureStatus }
  114.  
  115.  
  116. TYPE
  117. SIGCertStatus = INTEGER;
  118. SIGSignatureStatus = INTEGER;
  119.  
  120. { Gestalt selector code - returns toolbox version in low-order word }
  121. CONST
  122. gestaltDigitalSignatureVersion = 'dsig';
  123.  
  124.  
  125. { Number of bytes needed for a digest record when using SIGDigest }
  126. kSIGDigestSize = 16;
  127.  
  128. TYPE
  129. SIGDigestData = PACKED ARRAY[1..kSIGDigestSize] OF Byte;
  130. SIGDigestDataPtr = ^SIGDigestData;
  131.  
  132. SIGCertInfo = RECORD
  133.     startDate: LONGINT;                    { cert start validity date }
  134.     endDate: LONGINT;                    { cert end validity date }
  135.     certStatus: SIGCertStatus;            { see comment on SIGCertStatus for definition }
  136.     certAttributeCount: LONGINT;        { number of name attributes in this cert }
  137.     issuerAttributeCount: LONGINT;        { number of name attributes in this certs issuer }
  138.     serialNumber: Str255;                { cert serial number }
  139.     END;
  140.  
  141. SIGCertInfoPtr = ^SIGCertInfo;
  142.  
  143. SIGSignerInfo = RECORD
  144.     signingTime: LONGINT;                { time of signing }
  145.     certCount: LONGINT;                    { number of certificates in the cert set }
  146.     certSetStatusTime: LONGINT;            { Worst cert status time. See comment on
  147.                                             SIGCertStatus for definition }
  148.     signatureStatus: SIGSignatureStatus;{ The status of the signature. See comment on
  149.                                             SIGCertStatus for definition}
  150.     END;
  151.  
  152. SIGSignerInfoPtr = ^SIGSignerInfo;
  153.  
  154. SIGNameAttributesInfo = RECORD
  155.     onNewLevel: BOOLEAN;
  156.     attributeType: SIGNameAttributeType;
  157.     attributeScript: ScriptCode;
  158.     attribute: Str255;
  159.     END;
  160.  
  161. SIGNameAttributesInfoPtr = ^SIGNameAttributesInfo;
  162.  
  163. SIGContextPtr = Ptr;
  164. SIGSignaturePtr = Ptr;
  165.  
  166. {
  167. Certificates are always in order. That is, the signers cert is always 0, the
  168. issuer of the signers cert is always 1 etc… to the number of certificates-1.
  169. You can use this constant for readability in your code.
  170. }
  171.  
  172. CONST
  173. kSIGSignerCertIndex = 0;
  174.  
  175. {
  176. Call back procedure supplied by developer, return false to cancel the current process.
  177. }
  178.  
  179. TYPE
  180. SIGStatusProcPtr = ProcPtr;                { FUNCTION SIGStatusProcPtr(): BOOLEAN;}
  181.  
  182. {
  183. Resource id of standard signature icon suite, all sizes and colors are available.
  184. }
  185.  
  186. CONST
  187. kSIGSignatureIconResID            = -16797;
  188. kSIGValidSignatureIconResID        = -16799;
  189. kSIGInvalidSignatureIconResID    = -16798;
  190.  
  191. { ——————————————————————————————— CONTEXT CALLS ——————————————————————————————— }
  192. {
  193. To use the Digital Signature toolbox you will need a SIGContextPtr.  To create
  194. a SIGContextPtr you simply call SIGNewContext and it will create and initialize
  195. a context for you.  To free the memory occupied by the context and invalidate
  196. its internal data, call SIGDisposeContext. An initialized context has no notion
  197. of the type of operation it will be performing however, once you call
  198. SIGSignPrepare SIGVerifyPrepare, or SIGDigestPrepare, the contexts operation
  199. type is set and to switch  to another type of operation will require creating a
  200. new context. Be sure to pass the same context to corresponding toolbox calls
  201. (ie: SIGSignPrepare; SIGProcessData, SIGSign)  in other words mixing lets say
  202. signing and verify calls with the same context is not allowed.
  203. }
  204.  
  205.  
  206. FUNCTION SIGNewContext(VAR context: SIGContextPtr): OSErr;
  207.     INLINE $203C, 2, kSIGNewContext, $AA5D;
  208.  
  209. FUNCTION SIGDisposeContext(context: SIGContextPtr): OSErr;
  210.     INLINE $203C, 2, kSIGDisposeContext, $AA5D;
  211.  
  212.  
  213. { ——————————————————————————————— SIGNING CALLS ——————————————————————————————— }
  214. {
  215. Once you have created a SIGContextPtr, you create a signature by calling
  216. SIGSignPrepare once, followed by n calls to SIGProcessData, followed by one call
  217. to SIGSign. To create another signature on different data but for the same
  218. signer, don't dispose of the context and call SIGProcessData for the new data
  219. followed by a call SIGSign again. In this case the signer will not be prompted
  220. for their signer and password again as it was already provided.  Once you call
  221. SIGDisposeContext, all signer information will be cleared out of the context and
  222. the signer will be re-prompted.  The signer file FSSpecPtr should be set to nil
  223. if you want the toolbox to use the last signer by default or prompt for a signer
  224. if none exists.  The prompt parameter can be used to pass a string to be displayed
  225. in the dialog that prompts the user for their password.  If the substring "^1"
  226. (without the quotes) is in the prompt string, then the toolbox will replace it
  227. with the name of the signer from the signer selected by the user.  If an empty
  228. string is passed, the following default string will be sent to the toolbox
  229. "\pSigning as ^1.".  You can call any of the utility routines after SIGSignPrepare
  230. or SIGSign to get information about the signer or certs.
  231. }
  232.  
  233. FUNCTION SIGSignPrepare(context: SIGContextPtr; signerFile: FSSpecPtr;
  234.     prompt: StringPtr; VAR signatureSize: Size): OSErr;
  235.     INLINE $203C, 8, kSIGSignPrepare, $AA5D;
  236.  
  237. FUNCTION SIGSign(context: SIGContextPtr; signature: SIGSignaturePtr;
  238.     statusProc: SIGStatusProcPtr): OSErr;
  239.     INLINE $203C, 6, kSIGSign, $AA5D;
  240.  
  241.  
  242. { ——————————————————————————————— VERIFYING CALLS ——————————————————————————————— }
  243. {
  244. Once you have created a SIGContextPtr, you verify a signature by calling
  245. SIGVerifyPrepare  once, followed by n calls to SIGProcessData, followed by one
  246. call to SIGVerify. Check the return code from SIGVerify to see if the signature
  247. verified or not (noErr is returned on  success otherwise the appropriate error
  248. code).  Upon successfull verification, you can call any of the utility routines
  249. to find out who signed the data.
  250. }
  251.  
  252. FUNCTION SIGVerifyPrepare(context: SIGContextPtr; signature: SIGSignaturePtr;
  253.     signatureSize: Size; statusProc: SIGStatusProcPtr): OSErr;
  254.     INLINE $203C, 8, kSIGVerifyPrepare, $AA5D;
  255.  
  256. FUNCTION SIGVerify(context: SIGContextPtr): OSErr;
  257.     INLINE $203C, 2, kSIGVerify, $AA5D;
  258.  
  259. { —————————————————————————————— DIGESTING CALLS —————————————————————————————— }
  260. {
  261. Once you have created a SIGContextPtr, you create a digest by calling
  262. SIGDigestPrepare once, followed by n calls to SIGProcessData, followed by one
  263. call to SIGDigest.  You can dispose of the context after SIGDigest as the
  264. SIGDigestData does not reference back into it.  SIGDigest returns the digest in
  265. digest.
  266. }
  267.  
  268. FUNCTION SIGDigestPrepare(context: SIGContextPtr): OSErr;
  269.     INLINE $203C, 2, kSIGDigestPrepare, $AA5D;
  270.  
  271. FUNCTION SIGDigest(context: SIGContextPtr; digest: SIGDigestData): OSErr;
  272.     INLINE $203C, 4, kSIGDigest, $AA5D;
  273.  
  274.  
  275. { —————————————————————————————— PROCESSING DATA —————————————————————————————— }
  276. {
  277. To process data during a digest, sign, or verify operation call SIGProcessData
  278. as many times as necessary and with any sized blocks of data.  The data needs to
  279. be processed in the same order during corresponding sign and verify operations
  280. but does not need to be processed in the same sized chunks (i.e., the toolbox
  281. just sees it as a continuous bit stream).
  282. }
  283.  
  284. FUNCTION SIGProcessData(context: SIGContextPtr; data: UNIV Ptr; dataSize: Size): OSErr;
  285.     INLINE $203C, 6, kSIGProcessData, $AA5D;
  286.  
  287.  
  288. { ——————————————————————————————— UTILITY CALLS ——————————————————————————————— }
  289. {
  290. Given a context that has successfully performed a verification SIGShowSigner
  291. will  display a modal dialog with the entire distinguished name of the person
  292. who signed the data. the prompt (if supplied) will appear at the top of the
  293. dialog.  If no prompt is specified, the default prompt "\pVerification
  294. Successfull." will appear.
  295.  
  296. Given a context that has been populated by calling SIGSignPrepare, SIGSign or a
  297. successful SIGVerify, you can make the remaining utility calls:
  298.  
  299. SIGGetSignerInfo will return the SignerInfo record.  The certCount can be used
  300. to index into the certificate set when calling SIGGetCertInfo,
  301. SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes. The signingTime is
  302. only defined if the call is made after SIGSign  or SIGVerify. The certSetStatus
  303. will tell you the best status of the entire certificate set while
  304. certSetStatusTime will correspond to the time associated with that status (see
  305. definitions above).
  306.  
  307. SIGGetCertInfo will return the SIGCertInfo record when given a valid index into
  308. the cert set in  certIndex.  Note: The cert at index kSIGSignerCertIndex is
  309. always the signers certificate.  The  serial number, start date and end date
  310. are there should you wish to display that info.  The  certAttributeCount and
  311. issuerAttributeCount provide the number of parts in the name of that certificate
  312. or that certificates issuer respectively.  You use these numbers to index into
  313. either SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes to retrieve
  314. the name. The certStatus will tell you the status of the certificate while
  315. certStatusTime will correspond to the time associated with that status (see
  316. definitions above).
  317.  
  318. SIGGetCertNameAttributes and SIGGetCertIssuerNameAttributes return name parts
  319. of the certificate at  certIndex and attributeIndex.  The newLevel return value
  320. tells you wether the name attribute returned is at the same level in the name
  321. hierarchy as the previous attribute.  The type return value tells you  the type
  322. of attribute returned. nameAttribute is the actual string containing the name
  323. attribute.   So, if you wanted to display the entire distinguished name of the
  324. person who's signature was just validated you could do something like this;
  325.  
  326.     (…… variable declarations and verification code would preceed this sample ……)
  327.  
  328.     error = SIGGetCertInfo(verifyContext, kSIGSignerCertIndex, &certInfo);
  329.     HandleErr(error);
  330.  
  331.     for (i = 0; i <= certInfo.certAttributeCount-1; i++)
  332.         error = SIGGetCertNameAttributes(verifyContext, kSIGSignerCertIndex, i, &newLevel, &type, theAttribute);
  333.         HandleErr(error);
  334.         DisplayNamePart(theAttribute, type, newLevel);
  335. }
  336.  
  337. FUNCTION SIGShowSigner(context: SIGContextPtr; prompt: StringPtr): OSErr;
  338.     INLINE $203C, 4, kSIGShowSigner, $AA5D;
  339.  
  340. FUNCTION SIGGetSignerInfo(context: SIGContextPtr; VAR signerInfo: SIGSignerInfo): OSErr;
  341.     INLINE $203C, 4, kSIGGetSignerInfo, $AA5D; 
  342.  
  343. FUNCTION SIGGetCertInfo(
  344.         context: SIGContextPtr; certIndex: LONGINT; VAR certInfo: SIGCertInfo): OSErr;
  345.     INLINE $203C, 6, kSIGGetCertInfo, $AA5D; 
  346.  
  347. FUNCTION SIGGetCertNameAttributes(context: SIGContextPtr; certIndex: LONGINT;
  348.         attributeIndex: LONGINT; VAR attributeInfo: SIGNameAttributesInfo): OSErr;
  349.     INLINE $203C, 8, kSIGGetCertNameAttributes, $AA5D; 
  350.  
  351. FUNCTION SIGGetCertIssuerNameAttributes(context: SIGContextPtr; certIndex: LONGINT;
  352.         attributeIndex: LONGINT; VAR attributeInfo: SIGNameAttributesInfo): OSErr;
  353.     INLINE $203C, 8, kSIGGetCertIssuerNameAttributes, $AA5D; 
  354.  
  355.  
  356.  
  357. { ——————————————————————————— FILE SIGN & VERIFY CALLS —————————————————————————— }
  358. {
  359. These calls allow you to detect the presence of a standard signtaure in a file as
  360. well as sign and verify files in a standard way.  An example of this is the Finder,
  361. which uses these calls to allow the user to "drop sign" a file.
  362.  
  363. To detect if a file is signed in the standard way, pass the FSSpec of the file to SIGFileIsSigned.
  364. A result of noErr means the file is in fact signed, otherwise, a kSIGNoSignature error will
  365. be returned.
  366.  
  367. Once you have created a SIGContextPtr, you can make calls to either sign or verify a file in
  368. a standard way:
  369.  
  370. To sign a file, call SIGSignPrepare followed by 'n' number of calls to SIGSignFile,
  371. passing it the file spec for each file you wish to sign in turn.  You supply the context, the signature
  372. size that was returned from SIGSignPrepare and an optional call back proc.  The call will take care of all
  373. the processing of data and affixing the signature to the file. If a signature already exists in the file,
  374. it is replaced with the newly created signature.
  375.  
  376. To verify a file that was signed using SIGSignFile, call SIGVerifyFile passing it a new context and
  377. the file spec.  Once this call has completed, if the verification is successfull, you can pass the context
  378. to SIGShowSigner to display the name of the person who signed the file.
  379. }
  380.  
  381. FUNCTION SIGFileIsSigned(fileSpec: FSSpec): OSErr;
  382.     INLINE $203C, 2, kSIGFileIsSigned, $AA5D;
  383.  
  384. FUNCTION SIGSignFile(
  385.         context: SIGContextPtr; signatureSize: Size; fileSpec: FSSpec;
  386.         statusProc: SIGStatusProcPtr): OSErr;
  387.     INLINE $203C, 8, kSIGSignFile, $AA5D;
  388.  
  389. FUNCTION SIGVerifyFile(
  390.         context: SIGContextPtr; fileSpec: FSSpec; statusProc: SIGStatusProcPtr): OSErr;
  391.     INLINE $203C, 6, kSIGVerifyFile, $AA5D;
  392.  
  393.  
  394.  
  395. {$ENDC}    { UsingDigitalSignature }
  396.  
  397. {$IFC NOT UsingIncludes}
  398.     END.
  399. {$ENDC}
  400.